package com.javabasic.net.socket;


import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author xiongmin
 * @Description TODO
 * @Date 2019/10/22 14:02
 * @Version 1.0
 **/
public class Server {


    @Test
    public void main() {
        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(9999);
            while (true) {
                System.out.println("等待客户端连接");
                Socket client = serverSocket.accept();
                System.out.println("服务器接收到客户端连接");

                new Thread(() ->{
                    try {

                        System.out.println("等待客户端发送数据");
                        InputStream inputStream = client.getInputStream();
                        byte[] bytes = new byte[1024];
                        inputStream.read(bytes,0,bytes.length);
                        System.out.println("收到客户端数据");
                        String message = new String(bytes);
                        System.out.println(message.trim());
                        System.out.println("完成对客户端数据的接收");
                        inputStream.close();
                        client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }).start();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
